/**
 * 管理员的类
 *
 */
var Admin= function () {

    var actionHandler= function () {
      $(".ibox").on("click","a.dropdown-toggle",function () {
          if (!$(this).children().hasClass("fa-arrows")){
              return;
          }
          $("#adminModal").modal('show');
      });
      $('table').on("click","a.btn-primary",function () {
          if(!$(this).children().hasClass("fa-edit")){
              return;
          }
          var id= $(this).data("id");
          $("#personId").val(id);
          $("#changePasswordModal").modal("show");
      })
    };

    var validateHandler= function () {
          $('#submitForm').validate({
              rules:{
                  username:{
                      required:true,
                      remote:{
                          url:"validateName",
                          type:"get",
                      }
                  },
                  password:{
                      required:true,
                      minlength: 6,
                      maxlength: 20
                  },
                  endPassword:{
                      required:true,
                      equalTo:"#password"
                  }
              },
              messages:{
                  username:{
                      required:"必填",
                      remote:"该管理员已经存在"
                  },
                  password:{
                      required:"必填",
                      minlength:"至少有6位字",
                      maxlength:"最多20位字"
                  },
                  endPassword:{
                      required:"必填",
                      equalTo:"两次密码不一样"
                  }
              },
              submitHandler:function (form) {
                  $.ajax({
                      type:"get",
                      url:base+"/admin/common/public_key",
                      dataType:"json",
                      success:function (data) {
                          var password= $("#password").val();
                          var rsaKey= new RSAKey();
                          rsaKey.setPublic(b64tohex(data.modulus), b64tohex(data.exponent));
                          password = hex2b64(rsaKey.encrypt(password));
                          var params= {};
                          params.username= $("#username").val();
                          params.password= password;
                          params= JSON.stringify(params);
                          $.ajax({
                              type:"post",
                              url:"submit",
                              headers:{"token":token},
                              data:{params:params},
                              dataType:"json",
                              success:function (data) {
                                  Common.alert(data);
                                  if (data.success){
                                      setTimeout(function () {
                                          location.reload(true)
                                      },1500)
                                  }
                              }
                          })
                      }
                  })
              }
          })
    };

    var deleteHandler= function () {
       $("table").on("click","a.btn-primary",function () {
           if (!$(this).children().hasClass("fa-times-circle")) {
               return;
           }
           var id= $(this).data("id");
           if (id==null || id===""){
               return;
           }
           $.ajax({
               type:"post",
               url:"delete",
               data:{id:id},
               dataType:"json",
               headers:{"token":token},
               success:function (data) {
                   Common.alert(data);
                   if (data.success){
                       setTimeout(function () {
                           location.reload(true);
                       },1500);
                       return;
                   }
                   reloadToken();
               }
           })
       })
    };

    var changePasswordHandler= function () {
        $("#changePasswordForm").validate({
            rules:{
                password:{
                    required:true,
                    minlength:6,
                    maxlength:20
                },
                endPassword:{
                    required:true,
                    equalTo:"#originalpassword"
                }
            },
            messages:{
                password:{
                    required:"必填",
                    minlength:"最少要6位数",
                    maxlength:"最多要20位数"
                },
                endPassword:{
                    required:"必填",
                    equalTo:"两次密码不一致"
                }
            },
            submitHandler:function (form) {
                $.ajax({
                    type:"get",
                    url:base+"/admin/common/public_key",
                    dataType:"json",
                    success:function (data) {
                        var password= $("#originalpassword").val();
                        var rsaKey= new RSAKey();
                        rsaKey.setPublic(b64tohex(data.modulus), b64tohex(data.exponent));
                        var enPassword = hex2b64(rsaKey.encrypt(password));
                        var params={};
                        params.id= $("#personId").val();
                        params.password= enPassword;
                        params= JSON.stringify(params);
                        $.ajax({
                            type:"post",
                            url:"changePassword",
                            data:{"params":params},
                            dataType:"json",
                            headers:{"token":token},
                            success:function (data) {
                                Common.alert(data);
                                if (data.success){
                                    setTimeout(function () {
                                        location.reload(true);
                                    },1500)
                                    return;
                                }
                                reloadToken();
                            }
                        })
                    }
                })
            }
        })
    }

    return {
        init:function () {

            //事件的行为
            actionHandler();
            //校验表单

            validateHandler();

            //删除管理员
            deleteHandler();

            //修改密码
            changePasswordHandler();
        }
    }
}();